'use strict';
const moment = require('moment');
module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;
  const Customer = app.model.define('customer', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: STRING,
    },
    country: {
      type: STRING,
    },
    province: {
      type: STRING,
    },
    city: {
      type: STRING,
    },
    address: {
      type: STRING,
    },
    postcode: {
      type: STRING,
    },
    job: {
      type: STRING,
    },
    phone: {
      type: STRING,
    },
    email: {
      type: STRING,
    },
    fax: {
      type: STRING,
    },
    website: {
      type: STRING,
    },
    introduce: {
      type: STRING,
    },
    state: {
      type: INTEGER,
    },
    staff: {
      type: INTEGER,
    },
    protectstart: {
      type: DATE,
    },
    protectend: {
      type: DATE,
    },
    level: {
      type: INTEGER,
    },
    integral: {
      type: INTEGER,
    },
    created_at: {
      type: DATE,
      get() {
        return moment(this.getDataValue('created_at')).format(
          'YYYY-MM-DD HH:mm:ss'
        );
      },
    },
    updated_at: {
      type: DATE,
      get() {
        return moment(this.getDataValue('updated_at')).format(
          'YYYY-MM-DD HH:mm:ss'
        );
      },
    },
  });
  Customer.associate = () => {
    Customer.belongsToMany(app.model.Customer.Contact, {
      through: app.model.Customer.ContactCustomer,
      foreignKey: 'customer',
      otherKey: 'contact',
      as: 'Contacts',
    });
    Customer.hasMany(app.model.Customer.Examine, {
      foreignKey: 'customer_id',
    });
  };
  return Customer;
};
